<template>
  <div>
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avarar_box">
        <img class="logo" src="@/assets/img/logo.png" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="registerFormRef"
        label-width="0"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username" label="">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <div class="getcodeWrap">
          <el-form-item prop="phone" label="" class="getcodeBefore">
            <el-input
              v-model="loginForm.phone"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <div class="getcode" @click="getcode">
            <i class="i" v-if="count > 0">{{ count }}s后</i>{{ send }}
          </div>
        </div>
        <!-- 验证码 -->
        <el-form-item prop="code" label="">
          <el-input
            v-model="loginForm.code"
            placeholder="请输入验证码"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password" label="">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button class="button login" type="primary" @click="register"
            >注册</el-button
          >
        </el-form-item>
        <div class="goRegister" @click="join">已有账号，去登录</div>
      </el-form>
    </div>
    <el-dialog
      :visible.sync="centerDialogVisible"
      :width="width"
      center
      top="29vh"
    >
      <div class="center">城阳于氏族谱暂未收录您的</div>
      <div class="center">个人信息请联系管理员</div>
      <span slot="footer" class="dialog-footer">
        <div class="confirm" @click="centerDialogVisible = false">
          确 定
        </div>
      </span>
    </el-dialog>
    <div class="toost" v-if="toost">注册成功</div>
  </div>
</template>

<script>
// import mapMutations from 'vuex'
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        phone: '',
        code: ''
      },
      centerDialogVisible: false,
      count: 0,
      toost: '',
      send: '获取验证码',
      timer: null,
      timerR: null,
      width: '30%',
      /* 表单验证规则 */
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '请输入正确的验证码', trigger: 'blur' }
        ],
        phone: [
          {
            required: true,
            pattern: /^1[345678]\d{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    if (
      /Android|webOS| iPhone | iPad | iPod |BlackBerry|opera mini|opera mobile|appleWebkit.*mobile|mobile/i.test(
        navigator.userAgent
      )
    ) {
      this.width = '70%'
    } else {
      this.width = '30%'
    }
  },
  methods: {
    // ...mapMutations(['login', 'logout']),
    register() {
      // 注册事件(测试) 可根据接口/需求自行修改调试
      if (this.loginForm.username !== '于氏') {
        this.centerDialogVisible = true
        return
      }
      this.$refs.registerFormRef.validate(valid => {
        if (!valid) return
        this.toost = true
        this.timerR = setInterval(() => {
          clearInterval(this.timerR)
          this.toost = false
          this.$router.push('/login')
        }, 800)
      })
    },
    join() {
      // 已有账号点击跳转登录
      this.$router.push('/login')
    },
    getcode() {
      // 获取验证码 静态展示 可根据接口/需求自行修改调试
      let that = this
      this.$refs.registerFormRef.validateField('phone', valid => {
        if (valid) {
          return
        } else {
          that.count = 60
          that.send = '重新获取'
          that.timer = setInterval(() => {
            if (that.count > 0 && that.count <= 60) {
              that.count--
            } else {
              clearInterval(that.timer)
              that.timer = null
            }
          }, 1000)
        }
      })
    }
  }
}
</script>

<style lang="less">
.center {
  text-align: center;
}
.el-dialog {
  border-radius: 10px !important;
}
.el-dialog__footer {
  padding: 5px 0 5px 0 !important ;
  box-sizing: border-box;
}
.el-dialog__header {
  display: none;
}
.el-dialog__body {
  text-align: initial;
  padding: 18px 25px 10px !important;
}
.confirm {
  width: 100%;
  height: 36px;
  font-family: SourceHanSansCN-Regular;
  font-size: 15px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 36px;
  letter-spacing: 0px;
  color: #c70101;
  text-align: center;
  border-top: 1px solid #e5e5e5;
}
.getcodeWrap {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.getcodeBefore {
  width: 60%;
}
.getcode {
  width: 40%;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-family: SourceHanSansCN-Normal;
  font-size: 15px;
  font-weight: bold;
  font-stretch: normal;
  color: #7b7b7b;
  margin-bottom: 22.5px;
  border-bottom: 1px solid #dcdfe6;
  cursor: pointer;
}
.i {
  font-style: normal;
}
.loginWrap {
  width: 2rem;
  height: 3rem;
  background: rgba(0, 0, 0, 0.6);
  margin: 0 auto;
}
.login_box {
  width: 450px;
  // height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -71%);
  .avarar_box {
    width: 256px;
    height: 256px;
    // border: 1px solid #eee;
    // border-radius: 50%;
    padding: 10px;
    // box-shadow: 0 0 10px #ddd;
    margin: 0 auto;
    margin-bottom: 20px;
    // position: absolute;
    // left: 50%;
    // transform: translate(-50%, -50%);
    background-color: #fff;
    .logo {
      display: block;
      width: 100%;
      height: 100%;
      // border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  width: 300px;
  margin: 0 auto;
  .button {
    width: 100%;
    height: 45px;
    border-radius: 49px;
    margin: 0;
    border: none;
  }
  .login {
    background-color: #c70101;
    margin: 20px 0;
    &:hover {
      background-color: #c70101;
    }
  }
  .reset {
    background-color: #999999;
    &:hover {
      background-color: #999999;
    }
  }
}
.login_form {
  width: 100%;
  // position: absolute;
  // bottom: 0;
  padding: 0 20px;
  box-sizing: border-box;
}
.goRegister {
  width: 100%;
  height: 30px;
  font-family: SourceHanSansSC-Regular;
  font-size: 14px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 30px;
  letter-spacing: 0px;
  color: #999999;
  text-align: center;
  margin-top: 65px;
  cursor: pointer;
}
.login_form input {
  border: none;
  border-bottom: 1px solid #dcdfe6;
  border-radius: 0;
}
.toost {
  padding: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 12px;
  position: fixed;
  left: 50%;
  bottom: 30%;
  transform: translateX(-50%);
  transition: all 0.5s ease;
  border-radius: 10px;
}
@media screen and (max-width: 768px) {
  .login_box {
    width: 100%;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avarar_box {
      width: 100px;
      height: 100px;
    }
  }
}
</style>
